import { NavigationCallback } from './navigationCallback'
import { PretreatmentService } from './pretreatmenService';
import { HRouter } from "./hrouter";
import { Flags } from './Flags';
import { NavigationResult } from './navigationResult';
import { util } from '@kit.ArkTS';
import { GlobalContext } from './GlobalContext';

export class Postcard {
  private uri: string
  private params?: any
  private navigationCallback: NavigationCallback | null = null
  private pretreatmentService: PretreatmentService | null = null
  private greenChannel: boolean = false;
  private tag?: Object;
  private flags: number = 0;
  private rawUri?: string

  constructor(uri: string, params: any) {
    this.uri = uri;
    this.params = params;
  }

  setPretreatmentService(pretreatmentService: PretreatmentService): Postcard {
    this.pretreatmentService = pretreatmentService;
    return this;
  }

  getPretreatmentService(): PretreatmentService | null {
    if (!!this.pretreatmentService)
      return this.pretreatmentService
    else
      return null
  }

  setNavigationCallback(callback: NavigationCallback) {
    this.navigationCallback = callback;
  }

  getNavigationCallback(): NavigationCallback | null {
    if (!!this.navigationCallback)
      return this.navigationCallback
    else
      return null
  }

  setUri(uri: string): Postcard {
    this.uri = uri;
    return this;
  }

  withParams(params: Object): Postcard {
    this.params = params;
    return this;
  }

  setGreenChannel(greenChannel: boolean): Postcard {
    this.greenChannel = greenChannel;
    return this;
  }

  getGreenChannel(): boolean {
    return this.greenChannel
  }

  getParams(): Object {
    if (!!this.params) {
      return this.params;
    } else {
      return new Object();
    }
  }

  getUri(): string {
    return this.uri;
  }

  setOnResult<T>(navigationResult: NavigationResult<T>): Postcard {
    if (!this.params) {
      this.params = {}
    }
    this.params.navigationResult = util.generateRandomUUID(false)
    GlobalContext.getContext().setObject(this.params.navigationResult, navigationResult)
    return this
  }

  // 正常跳转
  navigation() {
    this.routers()
  }

  back() {
    HRouter.getInstance().back(this)
  }

  // 跳转回调
  navigationWithCallback(callback: NavigationCallback) {
    this.setNavigationCallback(callback);
    this.routers()
  }

  getTag(): Object | void {
    if (!!this.tag) {
      return this.tag;
    }
  }

  setTag(tag: Object): Postcard {
    this.tag = tag;
    return this;
  }

  withFlags(flags: number | boolean): Postcard {
    if (typeof flags == 'number') {
      this.flags = flags;
    } else {
      this.addFlags(Flags.REPLACE)
    }
    return this;
  }

  addFlags(flags: number): Postcard {
    this.flags |= flags;
    return this
  }

  getFlags(): number {
    return this.flags;
  }

  toString(): string {
    return "Postcard{" +
      "uri=" + this.uri +
      ", tag=" + this.tag +
      ", params=" + this.params!.toString() +
      ", flags=" + this.flags +
      ", greenChannel=" + this.greenChannel +
      "}\n";
  }

  private routers() {
    HRouter.getInstance().navigation(this, this.getNavigationCallback() as NavigationCallback)
  }

  _setRawUri(rawUri: string) {
    this.rawUri = rawUri
  }

  _getRawUri(): string | undefined {
    return this.rawUri
  }
}